/*
 * $Id: NumberOperator.java,v 1.6 2003/06/04 18:17:29 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.guts;

import java.util.Stack;
import java.text.NumberFormat;

/**
 * This class represents a number.
 * We pretend that numbers are just ordinary operators with
 * zero arity, this approach simplifies the parsing and evaluation
 * code.
 *
 * @author Tomas Znamenacek
 */
public class NumberOperator extends NullaryOperator {

	protected Double value;
	
	public NumberOperator(Number value) {

		this.value = new Double(value.doubleValue());
		this.priority = Integer.MAX_VALUE;
	}
	
	public int getPriority() {

		return priority;
	}

	public double evaluateSubTree() {

		return value.doubleValue();
	}

	public String toString(int parentPriority) {

		return format();
	}

	public String toTeX(int parentPriority) {
		
		return format();
	}

	public String toXML(int parentPriority) {

		return "<mn>" + format() + "</mn>";
	}

	public String toString() {

		return "<NumberOperator::" + format() + ">";
	}

	/**
	 * Formats value of this operator according to locale setting.
	 */
	protected String format() {

		return NumberFormat.getInstance().format(value);
	}
	
	public boolean equals(Object o) {

		return (o instanceof NumberOperator) ?
			(((NumberOperator) o).toString().equals(toString())) : false;
	}
}
